package com.example.whatrubbish.service.impl;

import com.example.whatrubbish.mapper.IntegrationMapper;
import com.example.whatrubbish.model.Integration;
import com.example.whatrubbish.service.IntegrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class IntegrationServiceImpl implements IntegrationService {
    @Autowired
    IntegrationMapper integrationMapper;

    @Override
    public Integration selectByUserId(String userId) {
        return integrationMapper.selectByUserId(userId);
    }

    @Override
    public int update(Integration integration) {
        Integration record = selectByUserId(integration.getUserId());
        if (Objects.isNull(record)) {
            integrationMapper.insertSelective(new Integration(null, integration.getScore(), integration.getUserId()));
        }
        return integrationMapper.updateByUserIdSelective(integration);
    }
}
